package com.tx.service.impl;

import com.tx.dao.UserDao;
import com.tx.entity.Page;
import com.tx.entity.Result;
import com.tx.entity.User;
import com.tx.service.UserService;
import com.tx.utils.PageUtils;
import com.tx.utils.SendCode;
import com.tx.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: tcx
 * @description: 用户信息业务层实现类
 * @Date: 2019/11/3
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /* *
     * @description:  用户登录实现
     * @author tcx
     * @date 2019/11/5
     * @param userName
     * @param uPassword
     * @return com.tx.entity.User
     */
    public User getUser(String userName, String uPassword) {
        //查询用户盐值
        String salt = userDao.selectUserSalt(userName);
        //加盐值加密后密码
        String pwd = StringUtil.encode(uPassword+salt);
        //传入用户名、密码进入数据库对比
        return userDao.selectUser(userName,pwd);
    }
    /* *
     * @description:  用户注册
     * @author tcx
     * @date 2019/11/14
     * @param user
     * @return void
     */
    public void add(User user) {
        //生成盐值
        String salt = StringUtil.salt();
        String pwd = StringUtil.encode(user.getuPassword()+salt);
        user.setuPassword(pwd);//存入加密后的密码
        user.setuSalt(salt);//存入盐值
        //获取用户注册时间
        user.setuCreateTime(new Date());
        userDao.insert(user);
    }

    public void edit(User user) {
        userDao.update(user);
    }

    public void remove(Integer uId) {
        userDao.delete(uId);
    }

    public User get(Integer uId) {
        return userDao.select(uId);
    }

    public List<User> getAll() {
        return userDao.selectAll();
    }

    public User getUserByName(String userName) {
        return userDao.selectByName(userName);
    }

    public User getUserByEmail(String uEmail) {
        return userDao.selectByEmail(uEmail);
    }

    /* *
     * @description:  更新用户头像并返回用户信息
     * @author tcx
     * @date 2019/11/15
     * @param user
     * @return com.tx.entity.User
     */
    public User editHeadImage(User user) {
        //更新用户头像
        userDao.updateHeadImage(user);
        //查询用户信息
        User user1 = userDao.select(user.getuId());
        return user1;
    }

    public Result getUserByPage(Page page) {
        //获得博客总记录数
        Integer count = userDao.selectUserCount();
        page = PageUtils.createPage(page,count);
        List<User> list = userDao.selectUserByPage(page);
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }
    //修改用户昵称
    public void editNickName(User user) {
        userDao.updateNickName(user);
    }
    //获得用户密码
    public String getPwd(Integer uId) {
        return userDao.selectPwd(uId);
    }
    //修改用户密码
    public Integer editPwd(Map<String,Object> map) {
        //重新生成盐值
        String salt = StringUtil.salt();
        //获得密码
        String pwd = StringUtil.encode((String) map.get("uPassword")+salt);
        map.put("uPassword",pwd);
        map.put("uSalt",salt);
        return userDao.updatePwd(map);
    }

    public int getAllUser() {
        return 0;
    }
    /* *
     * @description:  发送验证邮件
     * @author tcx
     * @date 2020/2/6
     * @param email
     * @return java.lang.String
     */
    public String sendEmail(String email,int flag) {
        //生成6位验证码
        String code = SendCode.randomCode();
        //发送邮件
        SendCode.sendEmail(email,code,flag);

        return code;
    }

    public Integer getTotal(Map<String, Object> map) {
        return userDao.getTotal(map);
    }

    public List<User> list(Map<String, Object> map) {
        return userDao.selectUserKey(map);
    }
    /**
     * 原始密码是否匹配
     * */
    public boolean rePwd(String pwd,User user) {
        //查询用户盐值
        String salt = userDao.selectUserSalt(user.getUserName());
        //查询用户原始密码
        String password = userDao.selectPwd(user.getuId());
        //加盐值加密后用户输入密码
        String pwd2=StringUtil.encode(pwd+salt);
        if (password.equals(pwd2)){
            return true;
        }
        return false;
    }

}
